package com.hhhn.wk.base;

import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.View;

import com.hhhn.wk.app.MyApplication;
import com.hhhn.wk.entity.User;
import com.hhhn.wk.config.Config;
import com.hhhn.wk.utils.NetBroadCastReciver;
import com.hhhn.wk.utils.SharePreferencesUser;
import com.hhhn.wk.utils.ToastUtils;
import com.hhhn.wk.widget.dialog.LoadDialog;
import com.hhhn.wk.widget.logger.AndroidLogAdapter;
import com.hhhn.wk.widget.logger.FormatStrategy;
import com.hhhn.wk.widget.logger.Logger;
import com.hhhn.wk.widget.logger.PrettyFormatStrategy;
import com.umeng.analytics.MobclickAgent;
//import com.umeng.message.PushAgent;

import java.text.DecimalFormat;


/**
 * * * * * * * * * * * *
 * @author：WenKe ｡◕‿◕｡ *
 * Data：2017     ｡◕‿◕｡ *
 * QQ：370820580  ｡◕‿◕｡ *
 * Function：     ｡◕‿◕｡ *
 *
 * * * * * * * * * * * */

public abstract class BaseActivity extends FragmentActivity {
    NetBroadCastReciver br;
    protected MyApplication myApp = null;
    private LoadDialog dialog=null;
    private boolean isDebug;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getMyApp();
        LoadDialog.Builder customBuilder = new LoadDialog.Builder(this);
        dialog = customBuilder.create();
        goNetBroadCastReciver();
        getMyApp().addActivity(this);
        isDebug=getMyApp().getIsDebug();
//      PushAgent.getInstance(BaseActivity.this).onAppStart();
        //封装OnCreate 用于绑定xml和初始化类
        setOnCreateContentView();
        //封装initView；用于findViewById控件和初始化数据
        initView();
        //封装initData；用于填充数据
        initData();
        //封装initData；用于监听
        initClick(); 
    }
    //初始化网络监听
    private void goNetBroadCastReciver() {
        br=new NetBroadCastReciver(this);
        IntentFilter itFilter = new IntentFilter();
        itFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
        registerReceiver(br, itFilter);
    }
    //初始化app
    public MyApplication getMyApp() {
        if(myApp == null)
        {
            myApp = (MyApplication) getApplication();
        }
        return myApp;
    }
    protected abstract void setOnCreateContentView();
    protected abstract void initView();
    protected abstract void initData();
    protected abstract void initClick();
    @Override
    protected void onResume() {
        if (br!=null){
            unregisterReceiver(br);
        }
        br=new NetBroadCastReciver(this);
        IntentFilter itFilter = new IntentFilter();
        itFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
        registerReceiver(br, itFilter);
        super.onResume();
        MobclickAgent.onResume(this);
    }
    @Override
    protected void onDestroy() {

        if (dialog != null){
            dialog.dismiss();
        }
        unregisterReceiver(br);
        getMyApp().removeActivity(this);
        super.onDestroy();
    }
    /***
     * 封装一键调用
     */
    //封装显示小人
    public void showLoad() {
        dialog.show();
    }
    //封装关闭小人
    public void dismissLoad() {
        dialog.dismiss();
    }
    //封装页面关闭按钮
    public void gofinish(View view){
        finish();
    }
    //封装获取User实体类
    public User getUser() {
        User user=null;
        if (SharePreferencesUser.getBean(this, Config.USER)!= null) {
            return (User) SharePreferencesUser.getBean(this, Config.USER);
        }else {
            return user ;
        }
    }
    //封装Tost
    public void toastWk(String text) {
        ToastUtils.showShortToast(this,text);
    }
    //封装log日志
    public void LogWk(String msg) {
        if (isDebug) {
//            Log.d(Config.APP_NAME, msg);
            FormatStrategy formatStrategy = PrettyFormatStrategy.newBuilder()
                    .showThreadInfo(true)
                    .methodCount(1)
                    .tag(Config.APP_NAME)
                    .build();
            Logger.addLogAdapter(new AndroidLogAdapter(formatStrategy));
            Logger.d(msg);
        }
    }
    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }
}
